﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyAdmin_PhanQuyen_AM : System.Web.UI.Page
{
    // Các phần còn thiếu:
	//+Xử lý chỉ cho Admin vào trang PhanQuyen (tạo folder PhanQuyen và dùng Membership)
	//+Kiểm tra quyền đối với từng trang ở MasterPage
    protected Dictionary<string, object[]> filesAndControls;

    protected void Page_Load(object sender, EventArgs e)
    {
        roleCBX.AutoPostBack = true;
        
        if (!IsPostBack)
        {
            roleCBX.DataSource = DataAccess_Layer.RoleDAC.layHetTruAdmin();
            roleCBX.DataTextField = "RoleName";
            roleCBX.DataValueField = "RoleId";
            roleCBX.DataBind();                       
        }
        
        init();      
    }
    public void init()
    {
        filesAndControls = new Dictionary<string, object[]>();
        foreach (var file in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "MyAdmin"))
        {
            if (file.EndsWith(".aspx"))
            {
                string fileName = file.Substring(file.LastIndexOf('\\') + 1);
                DataAccess_Layer.PhanQuyen phanQuyenXem = DataAccess_Layer.PhanQuyenDAC.layPhanQuyen(fileName, roleCBX.SelectedValue, Quyen_.QuyenXem);
                DataAccess_Layer.PhanQuyen phanQuyenChinhSua = DataAccess_Layer.PhanQuyenDAC.layPhanQuyen(fileName, roleCBX.SelectedValue, Quyen_.QuyenChinhSua);

                TableRow r = createLabel(fileName);
                CheckBox xemCHECK = createCheckBox(fileName, r, "XemCHECK");
                CheckBox modifyCHECK = createCheckBox(fileName, r, "ModifyCHECK");

                xemCHECK.Checked = (phanQuyenXem != null);
                modifyCHECK.Checked = (phanQuyenChinhSua != null);

                table.Rows.Add(r);

                filesAndControls.Add(file, new object[] { xemCHECK, modifyCHECK, phanQuyenXem, phanQuyenChinhSua });
            }
        }
    }
    private static CheckBox createCheckBox(string fileName, TableRow r, string cbxName)
    {
        TableCell c = new TableCell();
        CheckBox checkBox = new CheckBox();
        checkBox.ID = fileName + cbxName;        
        c.Controls.Add(checkBox);
        r.Cells.Add(c);
        return checkBox;
    }
    private static TableRow createLabel(string fileName)
    {
        TableRow r = new TableRow();
        TableCell c1 = new TableCell();
        Label tenTrangLABEL = new Label();
        tenTrangLABEL.ID = fileName + "LABEL";
        c1.Controls.Add(tenTrangLABEL);
        r.Cells.Add(c1);
        tenTrangLABEL.Text = fileName;
        return r;
    }

    protected void saveBUTTON_Click1(object sender, EventArgs e)
    {
        foreach (var file in filesAndControls.Keys)
        {
            string fileName = file.Substring(file.LastIndexOf('\\') + 1);

            bool isAllowView = ((CheckBox)filesAndControls[file][0]).Checked;// checkbox trong trang phân quyền
            bool isAllowModify = ((CheckBox)filesAndControls[file][1]).Checked;// checkbox trong trang phân quyền

            DataAccess_Layer.PhanQuyen phanQuyenXem = (DataAccess_Layer.PhanQuyen)filesAndControls[file][2];// phân quyền đang tồn tại
            DataAccess_Layer.PhanQuyen phanQuyenChinhSua = (DataAccess_Layer.PhanQuyen)filesAndControls[file][3];// phân quyền đang tồn tại

            if (phanQuyenXem != null && !isAllowView)
            {
                DataAccess_Layer.PhanQuyenDAC.xoa(phanQuyenXem);
            }
            
            if(phanQuyenXem == null && isAllowView){
                DataAccess_Layer.PhanQuyen pqXem = createPhanQuyen(fileName, Quyen_.QuyenXem);
                DataAccess_Layer.PhanQuyenDAC.them(pqXem);
            }

            if (phanQuyenChinhSua != null && !isAllowModify)
            {
                DataAccess_Layer.PhanQuyenDAC.xoa(phanQuyenChinhSua);
            }

            if (phanQuyenChinhSua == null && isAllowModify)
            {
                DataAccess_Layer.PhanQuyen pqChinhSua = createPhanQuyen(fileName, Quyen_.QuyenChinhSua);

                DataAccess_Layer.PhanQuyenDAC.them(pqChinhSua);
            }
        }
    }
    private DataAccess_Layer.PhanQuyen createPhanQuyen(string fileName, int quyen)
    {
        DataAccess_Layer.PhanQuyen pq = new DataAccess_Layer.PhanQuyen();
        pq.RoleID = new Guid(roleCBX.SelectedValue);
        pq.Trang = fileName;
        pq.Quyen = quyen;
        return pq;
    }

    private static class Quyen_
    {
        private static int quyenXem = 0;

        public static int QuyenXem
        {
            get { return Quyen_.quyenXem; }
            set { Quyen_.quyenXem = value; }
        }
        private static int quyenChinhSua = 1;

        public static int QuyenChinhSua
        {
            get { return Quyen_.quyenChinhSua; }
            set { Quyen_.quyenChinhSua = value; }
        }
    }
    protected void roleCBX_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = Request.Url.ToString();
        if (url.IndexOf('?') != -1)
        {
            url = url.Remove(url.IndexOf('?'));
        }

        string newURL = url.Substring(url.LastIndexOf('/') + 1) + "?Index=" + roleCBX.SelectedIndex;
        Response.Redirect(newURL);
    }
    protected void roleCBX_DataBinding(object sender, EventArgs e)
    {        
        if (Request.Params["Index"] != null)
        {
            int index = int.Parse(Request.Params["Index"]);
            roleCBX.SelectedIndex = index;
        }
    }
}